
package com.work.mvc.view.panels;

import java.util.*;

import com.vaadin.data.util.HierarchicalContainer;
import com.vaadin.server.ThemeResource;
import com.vaadin.ui.*;
import com.work.constants.view.*;
import com.work.jaxb.menu.*;
import com.work.mvc.view.tree.*;

public class MenuPanel extends VerticalLayout {

	private static final long serialVersionUID = 5005244561057906279L;

	private Accordion accordionMenu = new Accordion();

	private MenuTreeFactory treeFactory = MenuTreeFactory.getInstance();

	public MenuPanel()
	{
		init();
	}

	private void init()
	{
		createMenu();

		this.setWidth(ComponentsSize.MENU_WIDTH);
		this.setHeight(ComponentsSize.ALMOST_FULL);
		this.addComponent(accordionMenu);
		this.setMargin(true);
	}

	public void createMenu()
	{
		if (ComponentsSources.rootMenu != null && ComponentsSources.rootMenu.getMenuList() != null) {
			List<XmlPropertyEnum> propertyEnumList = new ArrayList<>();
			propertyEnumList.add(XmlPropertyEnum.NAME);
			
			for (XmlMenu menu : ComponentsSources.rootMenu.getMenuList()) {
				if (menu.getActive()){
					Tree tree = new Tree();
					
					TreeController controller = (TreeController) menu.getControllerInstance();
					controller.setTree(tree);
					
					HierarchicalContainer treeContainer = treeFactory.createTreeContent(menu, propertyEnumList);
					tree.setContainerDataSource(treeContainer);
					tree.addValueChangeListener(controller);
					tree.setImmediate(true);
					
					tree.setItemCaptionPropertyId(XmlPropertyEnum.NAME.getValue());
					accordionMenu.addTab(tree, menu.getName(), new ThemeResource(ComponentsSources.ICON_PATH + menu.getIcon()));
				}
			}
		}
		
		accordionMenu.setSizeFull();
	}
}
